package com.bolt.report.engine;

import com.bolt.common.utils.StrUtil;
import com.bolt.report.exception.ReportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractBeanDataSource implements BRDataSource {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    protected String getElementName(BRField element) {
        if (element.getExpression() != null && StrUtil.isNotBlank(element.getExpression().getExpr())) {
            return element.getExpression().getExpr();
        } else {
            return element.getName();
        }
    }

    protected Object getFieldValue(Object bean, BRField element) throws ReportException {
        return getFieldValue(bean, getElementName(element));
    }

    protected Object getFieldValue(Object bean, String expression) throws ReportException {
        Object value ;
        try {
            value = ObjectFactory.getValueByExpression(expression,bean);
        } catch (Exception ex) {
            logger.warn("Error retrieving field value from bean:{}", new Object[]{expression}, ex);
            value = StrUtil.EMPTY;
        }
        return value;
    }

}